//
//  AllDataHandle.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "AllDataHandle.h"

@interface AllDataHandle ()

// 声明一个数组保存数组
@property (nonatomic, strong) NSMutableArray *dataArray;
// 声明一个数组保存每个数组的标题
@property (nonatomic, strong) NSMutableArray *titleArray;

// 声明一个数组保存详情界面的model
@property (nonatomic, strong) NSMutableArray *detailDataArray;

// 声明一个数组保存详情界面的Steps
@property (nonatomic, strong) NSMutableArray *stepsArray;

@end

@implementation AllDataHandle

+ (AllDataHandle *)shareAllDataHandle
{
    static AllDataHandle *allHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        allHandle = [[AllDataHandle alloc] init];
    });
    return allHandle;
}

// 请求数据并且返回刷新
- (void)requestAllDataAndReturn:(void (^)(NSArray *))block
{
    self.dataArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 请求数据
        NSURL *url = [NSURL URLWithString:kAllDataURL];
        NSMutableURLRequest *requeset = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
        [requeset setHTTPMethod:@"GET"];
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:requeset completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (!data) {
                return;
            }
            // NSLog(@"data = %@", data);
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *resultArray = [dic valueForKey:@"result"];
            for (NSDictionary *tempdic in resultArray) {
                [self.dataArray addObject:[tempdic valueForKey:@"list"]];
                [self.titleArray addObject:[tempdic valueForKey:@"name"]];
            }
            //        NSLog(@"self.dataArray = %@", self.dataArray);
            //        NSLog(@"self.titleArray = %@", self.titleArray);
            dispatch_async(dispatch_get_main_queue(), ^{
                 block(self.titleArray);
            });
           
            
        }];
        [dataTask resume];
    });
    
   
}

// 根据indexpath返回相应的数组
- (NSArray *)returnArrayByIndexPath:(NSIndexPath *)indexPath
{
    if (!(self.dataArray.count > indexPath.row)) {
        return nil;
    }
    return self.dataArray[indexPath.row];
}

// 根据indexPath返回title
- (NSString *)returnTitleByIndexPath:(NSIndexPath *)indexPath
{
    if (!(self.titleArray.count > indexPath.row)) {
        return nil;
    }
    return self.titleArray[indexPath.row];
}

// 返回cell的数量
- (NSInteger)returnNumberOfCell
{
    return self.dataArray.count;
}

// 根据indexPath和点击的button判断是第几个元素
- (NSInteger)returnIntegerByIndexPath:(NSInteger)row AndButtonsTag:(NSInteger)tag
{
//    // 定义一个数字记录count
//    NSInteger count = 0;
//    // 定义一个数组存储dataArray中不同数组的元素个数
//    NSMutableArray *countArray = [NSMutableArray array];
//    for (NSArray *array in self.dataArray) {
//        NSNumber *number = [NSNumber numberWithInteger:array.count];
//        [countArray addObject:number];
//    }
//    NSLog(@"countArray = %@", countArray);

    // 根据row判断是第几个Array
    NSArray *rowArray = self.dataArray[row];
    // 根据tag判断是第几个字典
    NSDictionary *dic = rowArray[tag];
    
    
    return [[dic valueForKey:@"id"] integerValue];
}



#pragma mark - 详情界面的数据
// 请求详情界面的数据并且返回
- (void)requestDetailAllDataByIndex:(NSInteger)index AndReturn:(void (^)(NSArray *array))block
{
    self.detailDataArray = [NSMutableArray array];
    self.stepsArray = [NSMutableArray array];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 请求数据
        NSString *urlString = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/index?key=a8be7fa328b601bbb1e55ef1084f9bbb&cid=%ld", (long)index];
        NSURL *url = [NSURL URLWithString:urlString];
        NSMutableURLRequest *requeset = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
        [requeset setHTTPMethod:@"GET"];
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:requeset completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (!data) {
                return;
            }
           // NSLog(@"detaildata = %@", data);
            
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *resultDic = [dic valueForKey:@"result"];
            NSArray *dataArray = [resultDic valueForKey:@"data"];
            for (NSDictionary *tempDic in dataArray) {
                AllNextModel *model = [[AllNextModel alloc] init];
                [model setValuesForKeysWithDictionary:tempDic];
                [self.detailDataArray addObject:model];
                [self.stepsArray addObject:[tempDic valueForKey:@"steps"]];
            }
          //  NSLog(@"self.detailDataArray = %@", self.detailDataArray);
            NSLog(@"self.stepsArray = %@", self.stepsArray);
            
            
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                block(self.detailDataArray);
            });
            
            
        }];
        [dataTask resume];
    });

}


// 返回详情cell的个数
- (NSInteger)returnCountOfDetailCell
{
    return self.detailDataArray.count;
}
// 根据indexpath返回model
- (AllNextModel *)returnDetailModelByIndexPath:(NSIndexPath *)indexPath
{
    if (!(self.detailDataArray.count > indexPath.row)) {
        return nil;
    }
    return self.detailDataArray[indexPath.row];
}

// 根据indexpath返回stepArray
- (NSArray *)returnStepArrayByIndexPath:(NSIndexPath *)indexPath
{
    if (!(self.stepsArray.count > indexPath.row)) {
        return nil;
    }
    return self.stepsArray[indexPath.row];
}





@end
